package io.jboot.fly.directive;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.fly.model.Ansers;
import io.jboot.fly.model.Post;
import io.jboot.fly.service.CategoryService;
import io.jboot.fly.service.PostService;
import io.jboot.fly.service.UserService;
import io.jboot.utils.ArrayUtils;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @version V1.0
 * @Title: 最新回答
 * @Package io.jboot.fly.directive
 */
@JFinalDirective("latestAnsers")
public class LatestAnsers extends JbootDirectiveBase {

    @Inject
    PostService postService;

    @Inject
    UserService userService;

    @Inject
    CategoryService categoryService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
        List<Ansers> latestAnsersList=new ArrayList<Ansers>();
        List<Record> records=Db.find("select u.nickname,u.avatar,p.title from `comment` c,post p,`user` u where c.post_id=p.id and c.user_id=u.id order by c.created desc  limit 5");
        for(Record record:records){
            String nickName=record.get("nickname");
            String avatar=record.get("avatar");
            String title=record.get("title");
            latestAnsersList.add(new Ansers(nickName,avatar,title));
        }
        if (ArrayUtils.isNotEmpty(latestAnsersList)) {
            scope.setLocal("ansers", latestAnsersList);
            renderBody(env, scope, writer);
        }
    }






    @Override
    public boolean hasEnd() {
        return true;
    }
}
